//
//  XGCollectionView.m
//  两种Cell间的切换
//
//  Created by 小果 on 16/7/14.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "XGCollectionView.h"
#import "CellModel.h"
#import "CollectionViewCell.h"

static NSString *ID = @"coll";
@interface XGCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) NSArray *collArray;
@end

@implementation XGCollectionView
-(NSArray *)collArray{
    if (nil == _collArray){
        NSString *path = [[NSBundle mainBundle] pathForResource:@"DataSource.json" ofType:nil];
        NSData *data = [NSData dataWithContentsOfFile:path];
        
        NSArray *dataAr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *mutab = [NSMutableArray array];
        
        [dataAr enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
            CellModel *model = [CellModel cellModelWithDict:dict];
            [mutab addObject:model];
        }];
        _collArray = mutab;
    }
    return _collArray;
}

-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    if (self = [super initWithFrame:frame collectionViewLayout:layout]){
    
        self.backgroundColor = Color;
        self.dataSource = self;
        self.delegate = self;
        
        [self registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:ID];
    }
    return self;
}

#pragma mark - collectionView的数据源方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.collArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    CellModel *model = self.collArray[indexPath.row];
    
    cell.collModel = model;
    
    
    return cell;
}
#pragma mark - Cell动画  willDisplayCell: cell将要显示时会调用的方法
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    // 缩放
    cell.layer.transform = CATransform3DMakeScale(0.7, 0.7, 1);
    [UIView animateWithDuration:1.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
    }];
    
}

@end
